package org.zsl.mybatis.app;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.zsl.mybatis.mapper.ProductMapper;
import org.zsl.mybatis.model.Product;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Client1 {

    public static void main(String[] args) throws IOException {


        /**
         * 思路2
         * */

        /**
         *  step1 :
         *     获得sqlSession
         *  step2：
         *     执行数据操作
         *
         *  step3 ：
         *      关闭sqlSession
         * */

        /**
         *  思路1：封装sqlSession的获取
         * */


        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession session = factory.openSession();

        ProductMapper mapper=  session.getMapper(ProductMapper.class);

        List<Product>  list= mapper.selectAll();

        for (Product product:list) {

            System.out.println(product);
        }

        session.close();


    }
}
